import os
import os
import csv
import math
import numpy as np
import matplotlib.pyplot as plt

# 函数：读取CSV文件
def read_csv(filename):
    try:
        data=[]
        with open(filename, 'r', newline='') as file:
            reader = csv.reader(file)
    
            for row in reader:
                if len(row)<4:
                    continue
                item=[]
                for cell in row:
                    item.append(float(cell))
                data.append(item)
            
        return data
    except FileNotFoundError:
        print(f"无法打开文件: {filename}")
        return []


def get_coeffs():
   
    coefficients_normal=[[],[],[],[]]    #c1k2,c1k2,c2k1,c2k2
    coefficients_tangent=[[],[],[],[]]   #c1k2,c1k2,c2k1,c2k2

    skif_c1=read_csv("C1.csv")
    skif_c2=read_csv("C2.csv")

    for i in range(0,len(skif_c1),4):
        coefficients_normal[0].append(skif_c1[i])
        coefficients_normal[1].append(skif_c1[i+1])
        coefficients_normal[2].append(skif_c2[i])
        coefficients_normal[3].append(skif_c2[i+1])

        coefficients_tangent[0].append(skif_c1[i+2])
        coefficients_tangent[1].append(skif_c1[i+3])
        coefficients_tangent[2].append(skif_c2[i+2])
        coefficients_tangent[3].append(skif_c2[i+3])


    return coefficients_normal,coefficients_tangent

def eval_polyval(R,coeffs,N):
    #print(coeffs)
    '''
    for f=sum(a_n*x^n),
    in py funciton, the coefficients of 'np.polyval' is: coefficients = [a_n, a_{n-1}, ..., a_1, a_0]
    in csv file, we assume that:coefficients = [a_0, a_{1}, ..., _{n-1}, a_n]
    ,so a re-order operation is needed.
    '''
    coeffs=[coeffs[len(coeffs)-1-i] for i in range(len(coeffs))]

    
    coefficients =[float(x) for x in coeffs]
    poly_value = np.polyval(coefficients, R)
    return poly_value

def eval_K(E,_2a,D,N,coefficients_normal,coefficients_tangent,weight_normal,weight_tangent):
    print(weight_tangent)
    print(weight_normal)

    show_debug_info=True
    #裂纹相对尺寸，用来多项式插值
    R=_2a/D
    coefficients_normal_k=[]
    coefficients_tangent_k=[]

    k_component_N=[]
    k_component_T=[]

    k_index=0
    for coeffs in coefficients_normal:
        k_component_item=[]
        k_component_item_c=[]
        #载荷分解系数
        k_index=k_index+1
        K_list=[]
        for i in range(N):#每一介
            coeff=coeffs[i]  
         
            #eval each order of HC function
            K=eval_polyval(R,coeff,N)/math.sqrt(D/2)*E
            k_component_item.append(K)
            K_list.append(K)

        if show_debug_info:
            print(f"index={k_index},k={K_list}")

        K=0.0
        for i in range(N):#每一介
            w=weight_normal[i]
            K=K+K_list[i]*w
            k_component_item_c.append(w)
            #print("N k item=",K_list[i],",coeff=",w,",VALUE=",K_list[i]*w)
        coefficients_normal_k.append(K)
        k_component_N.append(k_component_item)
        k_component_N.append(k_component_item_c)
        

    if show_debug_info:
        print("TANGENT:")

    #分别计算c1k2,c1k2,c2k1,c2k2
    k_index=0
    for coeffs in coefficients_tangent:
        k_component_item=[]
        k_component_item_c=[]
        k_index=k_index+1
        #载荷分解系数
        K_list=[]
        for i in range(N):#每一介
            coeff=coeffs[i]  
            #eval each order of HC function
            K=eval_polyval(R,coeff,N)/math.sqrt(D/2)*E
            k_component_item.append(K)
            K_list.append(K)

        K=0.0
        for i in range(N):#每一介
            w=weight_tangent[i]
            K=K+K_list[i]*w
            k_component_item_c.append(w)
            
        coefficients_tangent_k.append(K)
        k_component_T.append(k_component_item)
        k_component_T.append(k_component_item_c)
        
    K_all=[]
    for i in range(len(coefficients_normal_k)):
        K_all.append(coefficients_normal_k[i]+coefficients_tangent_k[i])

    return K_all

   
